package com.edu.lxshpc.service.impl;

import com.edu.lxshpc.pojo.User;
import com.edu.lxshpc.service.MyUserDetailsService;
import com.edu.lxshpc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author zgh
 * @date 2023/10/28
 */
@Service
@Slf4j
public class MyUserDetailsServiceImpl implements MyUserDetailsService {
    @Autowired
    private UserService userService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info("进入自定义登录验证用户[{}]",username);
        User user = userService.queryByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //角色名称集合
        ArrayList<GrantedAuthority> list = userService.getGrantedAuthorities(user);
        user.setAuthorities(list);
        return user;
    }

}
